Technical Q&A QA1018
Using AppleScript to send an email with an attachment


QÅFÇ«ÇÃÇÊǧDžÇ∑ÇÍÇŒÅAMac OS X ÇÃìdéqÉÅÅ[ÉãÉvÉçÉOÉâÉÄÇ…ìYïtèëófiïtÇ´ÇÃìdéqÉÅÅ[ÉãÇëóêMÇ∑ÇÈÇÊǧDžóvãÅÇ∑ÇÈ AppleScript ÇèëÇØÇÈÇ≈ǵÇÂǧǩÅB

AÅFMac OS X ÇÃìdéqÉÅÅ[ÉãÉvÉçÉOÉâÉÄÇÕ AppleScript Ç…ëŒâûǵǃǢNjÇ∑ÅBÉäÉXÉg 1 Ç…é¶Ç∑ÉXÉNÉäÉvÉgó·Ç≈ÇÕÅAÉÜÅ[ÉUÇ™ÅAëóêMÉÅÉbÉZÅ[ÉWÇ…ìYïtÇ∑ÇÈÉeÉLÉXÉgÉtÉ@ÉCÉãÇëIëÇ≈Ç´ÇÈÇÊǧDžǵǃǢNjÇ∑ÅB



tell application "Mail"
    (* ìdéqÉÅÅ[ÉãÉAÉvÉäÉPÅ[ÉVÉáÉìÇÃÉoÅ[ÉWÉáÉì  *)
    set mailversion to version as string

    (* ï\é¶Ç‹ÇΩÇÕëóêMÇÃèàóùÇéwíË - displayForManualSend
        Ç™ true Ç»ÇÁÇŒÅAÉÜÅ[ÉUÇ™ëóêMÇ≈Ç´ÇÈÇÊǧDžÅAÉÅÉbÉZÅ[ÉWÇ™
        ê∂ê¨Ç≥ÇÍÅAÉEÉBÉìÉhÉEÇ…ï\é¶Ç≥ÇÍÇ‹Ç∑ÅBfalse ÇÃèÍçáÅA
        ÉÅÉbÉZÅ[ÉWÇÕÇ∑ÇÆÇ…ëóÇÁÇÍÇ‹Ç∑ *)
    set displayForManualSend to true

    (* ÉÅÉbÉZÅ[ÉWÇÃàÍî ìIÇ»óvëfÇéwíË *)
    set bodyvar to return & return & "Test body."
    set addrVar to "bogus@apple.com"
    set addrNameVar to "Guinea Pig"

    (* ìYïtèëófiÇéwíË *)
    (* DZÇÃÉtÉ@ÉCÉãÉäÉXÉgÇÕǪÇÃëºÇÃê›íËÇé¶Ç∑ *)
    (* ìdéqÉÅÅ[ÉãÇëóêMÇ∑ÇÈÇΩÇflÇÃÉXÉNÉäÉvÉgéËèá *)

    (* íçà”ÅFmailto å`éÆÇà URL Ç≈ÇÕìYïtèëófiÇà *)
    (* éwíËÇ™Ç≈ǴǻǢ *)
    set fileAttachThis to choose file of type "TEXT"
    set fileList to {fileAttachThis}

    (* åèñºÇíËã` *)
    set subjectvar to "Test Message From AppleScript with Attachment!"

    (* ÉÅÉbÉZÅ[ÉWÇçÏê¨ *)
    set composeMessage to (a reference to (make new compose message ¬
        at beginning of compose messages))
    tell composeMessage
        make new to recipient at beginning of to recipients ¬
            with properties {address:addrVar, display name:addrNameVar}
        set the subject to subjectvar
        set the content to bodyvar
        tell content
            repeat with aFile in fileList
                make new text attachment ¬
                    with properties {file name:aFile} ¬
                    at before the first word of the ¬
                    first paragraph
            end repeat
        end tell
    end tell

    (* ÉÅÉbÉZÅ[ÉWÇëóêMÇ‹ÇΩÇÕï\é¶ *)
    if displayForManualSend then
        set messageEditor to make new message editor ¬
            at beginning of message editors

        (* éüÇ…ÅAìdéqÉÅÅ[ÉãÉAÉvÉäÉPÅ[ÉVÉáÉìÇÃÉoÅ[ÉWÉáÉì 
            1.0 ÇΔ 1.1. Ç…ÇÕdžÇÈÇ™à»ç~ÇÃÉoÅ[ÉWÉáÉìÇ≈èCê≥Ç≥ÇÍÇΩÉoÉOÇÃ
            âÒîï˚ñ@Çé¶Ç∑ *)
        if mailversion is "1.0" or mailversion is "1.1" then
            set compose message of last message editor to composeMessage
        else
            set compose message of first message editor to composeMessage
        end if
    else
        send composeMessage
    end if
end tell

ÉäÉXÉg 1Å@ìYïtèëófiïtÇ´ÇÃìdéqÉÅÅ[ÉãÇëóêMÇ∑ÇÈ AppleScript




[2002 îN 1 åé 4 ì˙]